<?php
/**
 * Created by PhpStorm.
 * User: henryxi-mac
 * Date: 2015/5/20
 * Time: 9:46
 */

error_reporting(E_ALL & ~E_NOTICE);
require_once dirname(__FILE__)."/lib/common.php";
require_once dirname(__FILE__)."/config.php";
global $db_config;
$db = new Mysql($db_config);
$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
$get_text = json_encode($_GET);
$dt = date("Y-m-d H:i:s",time());
$sql = "insert tickct_test(`VerifyTicket`,`get_text`,`time`) values('{$xml}','{$get_text}','{$dt}')";
$db->sql($sql);

include_once "wxBizMsgCrypt.php";

// 第三方发送消息给公众平台
$encodingAesKey = "8w6wGsRXSaNwlxd51kWvSdgyNHK8C12XsFDD0aeyr3t";
//{"signature":"35789d49615bd7490a738ff243624c81ac632ade","timestamp":"1432200014","nonce":"806618251","encrypt_type":"aes","msg_signature":"cf630bd7d6059e0dcd3e90a98a0e8c868f5e8071"}
$token = "350436_e";
$timeStamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$appId = "wx9dce312479a493dd";
$msg_sign = $_GET['msg_signature'];

$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$msg = '';
$errcode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $xml, $msg);

if ($errcode == 0){
    $xml_tree = new DOMDocument();
    $xml_tree->loadXML($msg);
    $array_t = $xml_tree->getElementsByTagName('ComponentVerifyTicket');
    $array_i = $xml_tree->getElementsByTagName('InfoType');
    $array_c = $xml_tree->getElementsByTagName('CreateTime');
    $ticket = $array_t->item(0)->nodeValue;
    $infoType = $array_i->item(0)->nodeValue;
    $createTime = $array_c->item(0)->nodeValue;

    $sql = "replace into authorize(`appid`,`VerifyTicket`,`InfoType`,`timestamp`) values('{$appId}','{$ticket}','{$infoType}','{$createTime}')";

    $db->sql($sql);
}
